/**
* 
*	UserController类
*	作者：邓磊
*	时间：2016年7月4日 16:40:43
*	作用：提供用户管理相关功能
*	域：/weixin/user
*
*/
package com.eptok.weixin.action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.hibernate.envers.tools.StringTools;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.eptok.weixin.util.WxCooikeManager;

import hikefa.core.remote.client.RemoteCall;
import hikefa.core.util.StringUtil;
import yspay.express.bean.Result;
import yspos.cpfs.enumeration.ExpInviteStatus;
import yspos.cpfs.enumeration.ExpSendMsgFuncState;
import yspos.cpfs.enumeration.ExpUserType;
import yspos.cpfs.express.entity.ExpBizMerchant;
import yspos.cpfs.express.entity.ExpExpressMerchant;
import yspos.cpfs.express.entity.ExpInviteUserinfo;
import yspos.cpfs.express.entity.ExpUserinfo;
import yspos.cpfs.express.entity.ExpWebchatUserinfo;
import yspos.cpfs.remoteserver.iface.IExpressService;
import yspos.remoteserver.iface.IYsServer;


@Controller
@RequestMapping("/weixin/user")
public class UserController  extends BaseDispatchAction{
	
	private static final Logger log = Logger.getLogger(UserController.class);
	private IExpressService expressServer = (IExpressService) RemoteCall.getRemoteObject(IExpressService.class);
	private IYsServer ysServer = (IYsServer) RemoteCall.getRemoteObject(IYsServer.class);
	
	/*
	 * 跳转至修改姓名，手机选择界面
	 */
	@RequestMapping(value = { "/toReBindSel.do" })    
	public ModelAndView toBindInfo(HttpServletRequest request, HttpServletResponse response,
			String state,String code) 
	{
		return new ModelAndView(PATH+"/user/reBindSel");
	}
	
	/*
	 * 重新绑定电话(旧手机号码) 
	 * 注意：两步 一次性完成
	 */
	@RequestMapping(value = { "/toReBindPhoneOld.do" })    
	public ModelAndView toReBindPhoneOld(HttpServletRequest request, HttpServletResponse response,
			String state,String code) 
	{
		request.setAttribute("user", WxCooikeManager.getUser(request));
		return new ModelAndView(PATH+"/user/reBindPhoneOld");
	}
	
	/*
	 * 重新绑定电话(新手机号码)
	 */
	@RequestMapping(value = { "/toReBindPhoneNew.do" })    
	public Object toReBindPhoneNew(HttpServletRequest request, HttpServletResponse response,String checkCode) 
	{
		ExpUserinfo user = WxCooikeManager.getUser(request);
		if(StringTools.isEmpty(checkCode))
		{
			return toRedirect("toReBindPhoneOld.do");
		}
		boolean flag = ysServer.validateMsg(ExpSendMsgFuncState.FUNC_SEND_BINDNEW.value, user.getPhone(), checkCode);
		if(flag)
		{
			return toFail(request, "1", "验证码已过期");
		}
		return new ModelAndView(PATH+"/user/reBindPhoneNew");
	}
	
	/*
	 * 重新绑定电话(新手机号码)
	 */
	@RequestMapping(value = { "/reBindPhoneNew.do" })    
	public ModelAndView reBindPhoneNew(HttpServletRequest request, HttpServletResponse response,
			String checkCode,String phone) 
	{
		try 
		{
			if(StringUtil.isEmpty(phone))
			{
				return toFail(request, "1", "电话不能为空");
			}
			expressServer.userinfoRebindPhone(WxCooikeManager.getWxUser(request).getOpenId(), phone);
			ExpUserinfo user = expressServer.userinfoGetByopid(WxCooikeManager.getWxUser(request).getOpenId());
			WxCooikeManager.setUser(response, user);
			return toSuccess(request, "", "绑定成功");
		} catch (Exception e) {
			log.error("重绑手机号失败："+e.getMessage());
			return toFail(request, "1", "绑定失败："+e.getMessage());
		}
	}
	

	/*
	 * 重新绑定姓名
	 */
	@RequestMapping(value = { "/toReBindName.do" })    
	public ModelAndView toReBindName(HttpServletRequest request, HttpServletResponse response,
			String state,String code) 
	{
		return new ModelAndView(PATH+"/user/reBindName");
	}
	
	@RequestMapping(value = { "/reBindName.do" })    
	public ModelAndView reBindName(HttpServletRequest request, HttpServletResponse response,String name) 
	{
		try {
			ExpWebchatUserinfo wxUser = WxCooikeManager.getWxUser(request);
			if(StringUtil.isEmpty(name))
			{
				return toFail(request, "1", "名字不能为空");
			}
			expressServer.userinfoRebindName(wxUser.getOpenId(), name);
			ExpUserinfo user = expressServer.userinfoGetByopid(wxUser.getOpenId());
			WxCooikeManager.setUser(response, user);
			return toSuccess(request, "0", "修改成功");
		} catch (Exception e) {
			log.error("重绑姓名失败："+e.getMessage());
			return toFail(request, "1", "绑定失败：请联系管理员");
		}
	}
	
	/*
	 * 升级成快递员/加入企业
	 */
	@RequestMapping(value = { "/toJoin.do" })    
	public ModelAndView toUpgrade(HttpServletRequest request, HttpServletResponse response,
			String invId) 
	{
				
		try {
			ExpInviteUserinfo invUser = expressServer.getInviteUserinfoById(invId);
			if(null == invUser)
			{
				return toFail(request, "1", "邀请关系不存在（"+invId+"）");
			}
			//已同意
			if(ExpInviteStatus.USE.value.equals(invUser.getState()))
			{
				return toFail(request, "1", "您已同意，无需再次操作");
			}
			//已拒绝
			if(ExpInviteStatus.REFUSE.value.equals(invUser.getState()))
			{
				return toFail(request, "1", "您已拒绝，不能再次操作");
			}
			//无效状态
			if(!ExpInviteStatus.UN_USE.value.equals(invUser.getState()))
			{
				return toFail(request, "1", "状态异常("+invId+":"+ExpInviteStatus.getCaption(invUser.getState())+")");
			}
			
			//通过
			//邀请加入企业
			if(invUser.getType().equals(ExpUserType.NOML_USER.value))
			{
				ExpBizMerchant bizMerc = expressServer.getBizMerchantByNo(invUser.getMercNo());
				if(null == bizMerc) {
					return toFail(request, "1", "企业商户不存在("+invUser.getMercNo()+")");
				}
				request.setAttribute("merc", bizMerc);
			}//邀请加入快递公司
			else if(invUser.getType().equals(ExpUserType.EXPR_USER.value))
			{
				ExpExpressMerchant expMerc = expressServer.getExpressMerchantByNo(invUser.getMercNo());
				if(null == expMerc) {
					return toFail(request, "1", "快递商户不存在("+invUser.getMercNo()+")");
				}
				request.setAttribute("merc", expMerc);
			}
			request.setAttribute("invUser", invUser);
		} catch (Exception e) {
			return toFail(request, "1", "程序异常，请联系管理员！");
		}
		
		return new ModelAndView(PATH+"/user/toJoin");
	}
	
	/*
	 * 邀请用户 成为快递员 或者 加入某企业  接受 或者拒绝
	 */
	@RequestMapping(value = { "/toJoinConf.do" })    
	public ModelAndView toUpgradeConf(HttpServletRequest request, HttpServletResponse response,
			String invId,String op) 
	{
		
		if(null == op)
		{
			return toFail(request, "1", "请选择接受或者拒绝");
		}
		ExpUserinfo user = WxCooikeManager.getUser(request);
		try {
			Result ret = expressServer.userInfoInvistConf(user, invId,op);
			if(ret.getCode().equals("0"))
			{
				user = expressServer.userinfoGetByid(user.getId());
				WxCooikeManager.setUser(response, user);
				return toSuccess(request, ret.getCode(), ret.getMsg());
			}else
			{
				return toFail(request, ret.getCode(), ret.getMsg());
			}
		} catch (Exception e) {
			e.printStackTrace();
			return toFail(request, "1", "操作失败："+e.getMessage());
		}
	}
}
